﻿using System.Diagnostics;
using System.Reflection;
using Microsoft.Win32;
using System.IO;
using System;

namespace Commands
{
    class StartupManager
    {
        public static void ShouldProgramAutostart(bool shouldAutostart)
        {
            if (shouldAutostart)
                SetAutoStart();
            else
                UnSetAutoStart();
        }

        /// <summary>
        /// Sets the autostart value for the assembly.
        /// </summary>
        public static void SetAutoStart()
        {
            _SetAutoStart(KEY_NAME, ASSEMBLY_LOCATION);
        }

        /// <summary>
        /// Unsets the autostart value for the assembly.
        /// </summary>
        public static void UnSetAutoStart()
        {
            _UnSetAutoStart(KEY_NAME);
        }

        #region private

        /// <summary>
        /// Returns whether auto start is enabled.
        /// </summary>
        private static bool IsAutoStartEnabled()
        {
            return _IsAutoStartEnabled(KEY_NAME, ASSEMBLY_LOCATION);
        }

        /// <summary>
        /// Returns whether auto start is enabled.
        /// </summary>
        /// <param name="keyName">Registry Key Name</param>
        /// <param name="assemblyLocation">Assembly location (e.g. Assembly.GetExecutingAssembly().Location)</param>
        private static bool _IsAutoStartEnabled(string keyName, string assemblyLocation)
        {
            Debug.Assert(!System.String.IsNullOrEmpty(keyName), "keyName is null or empty.");
            Debug.Assert(!System.String.IsNullOrEmpty(assemblyLocation), "assemblyLocation is null or empty.");

            RegistryKey key = Registry.CurrentUser.OpenSubKey(RUN_LOCATION);
            if (key == null)
                return false;

            string value = (string)key.GetValue(keyName);
            if (value == null)
                return false;

            return (value == assemblyLocation);
        }

        /// <summary>
        /// Sets the autostart value for the assembly.
        /// </summary>
        /// <param name="keyName">Registry Key Name</param>
        /// <param name="assemblyLocation">Assembly location (e.g. Assembly.GetExecutingAssembly().Location)</param>
        private static void _SetAutoStart(string keyName, string assemblyLocation)
        {
            Debug.Assert(!System.String.IsNullOrEmpty(keyName), "keyName is null or empty.");
            Debug.Assert(!System.String.IsNullOrEmpty(assemblyLocation), "assemblyLocation is null or empty.");

            if (IsAutoStartEnabled())
                return;

            RegistryKey key = Registry.CurrentUser.CreateSubKey(RUN_LOCATION);
            key.SetValue(keyName, assemblyLocation);
        }

        /// <summary>
        /// Unsets the autostart value for the assembly.
        /// </summary>
        /// <param name="keyName">Registry Key Name</param>
        private static void _UnSetAutoStart(string keyName)
        {
            Debug.Assert(!System.String.IsNullOrEmpty(keyName), "keyName is null or empty.");

            if (!IsAutoStartEnabled())
                return;
            
            RegistryKey key = Registry.CurrentUser.CreateSubKey(RUN_LOCATION);
            key.DeleteValue(keyName);
        }

        private const string KEY_NAME = "RemoteCommands";
        private static readonly string ASSEMBLY_LOCATION = Assembly.GetEntryAssembly().Location;
        private const string RUN_LOCATION = @"Software\Microsoft\Windows\CurrentVersion\Run";

        #endregion
    }
}
